//获取url上的请求参数
function getParams() {
    //获取问号及问号后面的内容
    var url = window.location.search;
    var params = new Object();
    if (url.indexOf("?") != -1) {
        //截取问号后面的内容,再使用&分割多个属性
        var arr = url.substr(1).split("&");
        for (var i = 0; i < arr.length; i++) {
            //使用=分割为keyvalue
            var keyValue = arr[i].split("=");
            params[keyValue[0]] = keyValue[1];
        }
    }
    return params;
}


//把表单input转为json对象
$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
}

//跳转到a链接中的地址
function skipHref() {
    var url = $(this).data("href");
    var id = $(this).data("id");
    //获取id值
    if(!url){
        var a = $(this).find('a');
        url = a.data("href");
        id = a.data("id");
    }
    window.location.href = url + "?id=" + id;
}
function isEnd() {
    return $(window).scrollTop() + $(window).height() >= $(document).height() -1;
}



//监听私信
var listen;
var url = "ws://192.168.17.224/socket/listen";
var theUser = sessionStorage.getItem("user");
theUser = JSON.parse(theUser);
var user1Id;
var user2Id;
var t;
if (theUser){
    if ('WebSocket' in window) {
        listen = new WebSocket(url); //建立连接
    } else if ('MozWebSocket' in window) {
        listen = new MozWebSocket(url);
    } else {
        alert('WebSocket is not supported by this browser.');
    };
    console.log("接收私信");

    listen.onopen = function (obj) {
        listen.send(""+theUser.id);
    };
    listen.onmessage = function (obj) {
        //$("#myModal").modal('hide');
        clearTimeout(t);
        var str = obj.data; //收到的信息
        var strs = str.split("&");
        //自己的id
        user1Id = strs[2];
        //对面的id
        user2Id = strs[1];
        if (sessionStorage.getItem(user2Id)) {
            sessionStorage.setItem(user2Id,sessionStorage.getItem(user2Id) + 1);
        }else{
            sessionStorage.setItem(user2Id,1);
        }
        $("#msgNum").html(sessionStorage.getItem(user2Id).length);
        $.get("/users/" + strs[1],function (data) {
            $("#source").html(data.nickName);
        });
        $("#myModal").modal("show");
        t = setTimeout(" $(\"#myModal\").modal('hide');",2000);
    };
}else  {
    console.log("未登录");
};
$(function () {
    $("#push").load("/pushModal.html");
});

function openMsg() {
    //向服务器发送开启私信的请求
    sessionStorage.setItem(user2Id,"");
    $.get("/letters/" + user1Id + "/" + user2Id,function (data) {
        var user1 = JSON.stringify(data.user1);
        var user2 = JSON.stringify(data.user2);
        sessionStorage.setItem("user1",user1);
        sessionStorage.setItem("user2",user2);
        location.href = "/mine/letter.html";
    })
}





